import requests
from bs4 import BeautifulSoup as bs
from pyquery import PyQuery as pq
import json
from sql_db_dy import MySqlHelper
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
headers = {
    'User-Agent': USER_AGENT
}


def read_bd_movie(url, movie_dict_all=None):
    if movie_dict_all is None:
        movie_dict_all = list()
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        j_obj = json.loads(response.text)
        num = 0
        all = j_obj['subjects']
        for a in all:
            title = a.get('title')
            grate = a.get('rate')
            movie_url = a.get('url')
            image_url = a.get('cover')
            movie_dict_all.append([title, grate, movie_url, image_url])
    else:
        print('你抓取的出问题了')
    return movie_dict_all


if __name__ == "__main__":
    for i in range(1, 6):
        url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&' \
          'page_start=' + str(i)
        print('33333333333333333333333333')
        print(url)
        movie_dict_all = read_bd_movie(url)
        mysql = MySqlHelper()
        sql = "INSERT INTO db_movie(title, grate, movie_url, image_url) VALUES(%s,%s,%s,%s)"
        print(movie_dict_all)
        if movie_dict_all:
            mysql.exec_many(sql, movie_dict_all)